package com.zhonghui.wms.controller;

import java.security.Security;
import java.util.Date;
import java.util.List;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.response.BaseResult;
import com.zhonghui.wms.domain.vo.WmsWarehousingApplicationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.wms.domain.WmsWarehousingApplication;
import com.zhonghui.wms.service.IWmsWarehousingApplicationService;
import com.zhonghui.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 智能仓储WMS-入库申请Controller
 *
 * @author zhonghui
 * @date 2022-05-25
 */
@Api(tags="智能仓储WMS-入库申请")
@RestController
@RequestMapping("/wms/warehousingApplication")
public class WmsWarehousingApplicationController extends BaseController
{
    @Autowired
    private IWmsWarehousingApplicationService wmsWarehousingApplicationService;

    /**
     * 查询智能仓储WMS-入库申请列表
     */
    @ApiOperation("查询智能仓储WMS-入库申请列表")
    @GetMapping("/list")
    public TableDataInfo<List<WmsWarehousingApplicationVo>> list(WmsWarehousingApplicationVo wmsWarehousingApplicationVo)
    {
        startPage();
        List<WmsWarehousingApplicationVo> list = wmsWarehousingApplicationService.selectWmsWarehousingApplicationList(wmsWarehousingApplicationVo);
        return getDataTable(list);
    }

    /**
     * 导出智能仓储WMS-入库申请列表
     */
    @ApiOperation("导出智能仓储WMS-入库申请列表")
    @Log(title = "智能仓储WMS-入库申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsWarehousingApplicationVo wmsWarehousingApplication)
    {
        List<WmsWarehousingApplicationVo> list = wmsWarehousingApplicationService.selectWmsWarehousingApplicationList(wmsWarehousingApplication);
        ExcelUtil<WmsWarehousingApplicationVo> util = new ExcelUtil<WmsWarehousingApplicationVo>(WmsWarehousingApplicationVo.class);
        util.exportExcel(response, list, "智能仓储WMS-入库申请数据");
    }

    /**
     * 获取智能仓储WMS-入库申请详细信息
     */
    @ApiOperation("获取智能仓储WMS-入库申请详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<WmsWarehousingApplicationVo> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(wmsWarehousingApplicationService.selectWmsWarehousingApplicationById(id));
    }

    /**
     * 新增智能仓储WMS-入库申请(保存)
     */
    @ApiOperation("新增智能仓储WMS-入库申请")
    @Log(title = "智能仓储WMS-入库申请", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody WmsWarehousingApplicationVo wmsWarehousingApplicationVo)
    {
        // 申请状态 0未提交 1待审核 2已审核
        wmsWarehousingApplicationVo.setApplicationStatus(Long.parseLong("0"));
        return BaseResult.success(wmsWarehousingApplicationService.insertWmsWarehousingApplication(wmsWarehousingApplicationVo));
    }

    /**
     * 修改智能仓储WMS-入库申请(保存)
     */
    @ApiOperation("修改智能仓储WMS-入库申请")
    @Log(title = "智能仓储WMS-入库申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody WmsWarehousingApplicationVo wmsWarehousingApplicationVo)
    {
        wmsWarehousingApplicationVo.setApplicationStatus(Long.parseLong("0"));
        wmsWarehousingApplicationVo.setApproved(null);
        wmsWarehousingApplicationVo.setApprovedStatus(null);
        wmsWarehousingApplicationVo.setApprovedDate(null);
        wmsWarehousingApplicationVo.setApprovedComments("");
        return BaseResult.success(wmsWarehousingApplicationService.updateWmsWarehousingApplication(wmsWarehousingApplicationVo));
    }

    /**
     * 提交智能仓储WMS-入库申请
     */
    @ApiOperation("提交智能仓储WMS-入库申请")
    @Log(title = "入库申请", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody WmsWarehousingApplicationVo wmsWarehousingApplicationVo) {
        wmsWarehousingApplicationVo.setApplicationStatus(Long.parseLong("1"));
        wmsWarehousingApplicationVo.setApplicationDate(DateUtils.getNowDate());
        if(wmsWarehousingApplicationVo.getId() != null) {
            // 修改提交
            WmsWarehousingApplicationVo applicationVo = wmsWarehousingApplicationService.selectWmsWarehousingApplicationById(wmsWarehousingApplicationVo.getId());
            if(applicationVo == null) {
                return BaseResult.error("此入库申请不存在，不能提交！");
            }
            wmsWarehousingApplicationVo.setApproved(null);
            wmsWarehousingApplicationVo.setApprovedStatus(null);
            wmsWarehousingApplicationVo.setApprovedDate(null);
            wmsWarehousingApplicationVo.setApprovedComments("");
            return BaseResult.success(wmsWarehousingApplicationService.updateWmsWarehousingApplication(wmsWarehousingApplicationVo));
        }else {
            // 申请状态 0未提交 1待审核 2已审核
            wmsWarehousingApplicationVo.setApplicationStatus(Long.parseLong("1"));
            // 新增提交
            return BaseResult.success(wmsWarehousingApplicationService.insertWmsWarehousingApplication(wmsWarehousingApplicationVo));
        }
    }

    /**
     * 审核智能仓储WMS-入库申请
     */
    @ApiOperation("审核智能仓储WMS-入库申请")
    @Log(title = "入库申请", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody WmsWarehousingApplicationVo wmsWarehousingApplicationVo) {
        wmsWarehousingApplicationVo.setApplicationStatus(Long.parseLong("2"));
        wmsWarehousingApplicationVo.setApprovedDate(DateUtils.getNowDate());
        wmsWarehousingApplicationVo.setApproved(SecurityUtils.getLoginUser().getUser().getUserId());
        return BaseResult.success(wmsWarehousingApplicationService.updateWmsWarehousingApplication(wmsWarehousingApplicationVo));
    }

    /**
     * 删除智能仓储WMS-入库申请
     */
    @ApiOperation("删除智能仓储WMS-入库申请")
    @Log(title = "智能仓储WMS-入库申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(wmsWarehousingApplicationService.deleteWmsWarehousingApplicationByIds(ids));
    }

    /**
     * 执行智能仓储WMS-入库申请-执行入库
     */
    @ApiOperation("执行智能仓储WMS-执行入库")
    @Log(title = "智能仓储WMS-入库申请-执行入库", businessType = BusinessType.INSERT)
    @PostMapping("/executeWarehousing")
    public BaseResult<Integer> executeWarehousing(@RequestBody WmsWarehousingApplicationVo wmsWarehousingApplicationVo)
    {
        wmsWarehousingApplicationService.executeWarehousing(wmsWarehousingApplicationVo.getId());
        return BaseResult.success();
    }
}
